import { createRouter, createWebHistory } from 'vue-router'
// import Layout from '@/views/layout/index.vue'
// import Home from '@/views/layout/home.vue'
// import Category from '@/views/layout/category.vue'
// import Cart from '@/views/layout/cart.vue'
// import User from '@/views/layout/user.vue'
// import Login from '@/views/login/index.vue'
// import Order from '@/views/order/index.vue'
// import Pay from '@/views/pay/index.vue'
// import Search from '@/views/search/index.vue'
// import ProDeatail from '@/views/prodetail/index.vue'
// import SearchList from '@/views/searchlist/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: '/',
    //   component: Layout,
    //   redirect: '/home',
    //   children: [
    //     { path: '/home', component: Home },
    //     { path: '/category', component: Category },
    //     { path: '/cart', component: Cart },
    //     { path: '/user', component: User }
    //   ]
    // },
    // { path: '/login', component: Login },
    // { path: '/pay', component: Pay },
    // { path: '/order', component: Order },
    // { path: '/search', component: Search },
    // { path: '/searchlist', component: SearchList },
    // { path: '/prodetail/:id', component: ProDeatail }
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        { path: '/home', component: () => import('@/views/layout/home.vue') },
        { path: '/category', component: () => import('@/views/layout/category.vue') },
        { path: '/cart', component: () => import('@/views/layout/cart.vue') },
        { path: '/user', component: () => import('@/views/layout/user.vue') }
      ]
    },
    { path: '/login', component: () => import('@/views/login/index.vue') },
    { path: '/pay', component: () => import('@/views/pay/index.vue') },
    { path: '/order', component: () => import('@/views/order/index.vue') },
    { path: '/search', component: () => import('@/views/search/index.vue') },
    { path: '/searchlist', component: () => import('@/views/searchlist/index.vue') },
    { path: '/prodetail/:id', component: () => import('@/views/prodetail/index.vue') }
  ],
})

// 全局前置守卫
const authUrl = ['/pay', '/order']
router.beforeEach((to, from, next) => {
   if (!authUrl.includes(to.path)) {
    next()
    return
   }
   const token = JSON.parse(localStorage.getItem('user'))?.userInfo.token || ''
   if (token) {
    next()
   } else {
    next('/login')
   }
})

export default router
